﻿using DemoQLKhoSX.DatabaseAccessLayer;
using DemoQLKhoSX.EntityClasses;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data;
using System.Text;

namespace DemoQLKhoSX.BusinessLogicLayer
{
    public class HangBLO
    {
        private HangDAO _HangDAO;
        public HangEntity _HangE;

        public HangBLO()
        {
            _HangDAO = new HangDAO();
            _HangE = new HangEntity();
        }

        public HangEntity getHangByID(string id)
        {
            DataTable tbl = new DataTable();
            tbl = _HangDAO.searchByID(id);

            if (tbl != null)
                foreach (DataRow r in tbl.Rows)
                {
                    _HangE.IDHang = id;
                    _HangE.ChatLieu = r["ChatLieu"].ToString();
                    _HangE.DonViTinh = r["DonViTinh"].ToString();
                    _HangE.Loai = r["Loai"].ToString();
                    _HangE.MoTaHang = r["MoTaHang"].ToString();
                    _HangE.Nguong = Convert.ToInt32(r["Nguong"]);
                    _HangE.NguonGoc = r["NguonGoc"].ToString();
                    _HangE.PhanLoai = r["PhanLoai"].ToString();
                    _HangE.Size = Convert.ToInt32(r["Size"]).ToString();
                    _HangE.SoLuongTon = Convert.ToInt32(r["SoLuongTon"]);
                    _HangE.TenHang = r["TenHang"].ToString();
                }
            return _HangE;
        }


        #region"Methods"
        public bool InsertHangB()
        {
            if (!_HangDAO.InsertHang(_HangE))
            {
                MessageBox.Show("Lưu thất bại");
                return false;
            }
            return true;
        }

        public bool DeleteHangB(string str)
        {
            if (!_HangDAO.DeleteHang(str))
            {
                MessageBox.Show("Xóa thất bại");
                return false;
            }
            else
                MessageBox.Show("Xóa thành công");
            return true;
        }

        public bool UpdateHangB()
        {
            if (!_HangDAO.UpdateHang(_HangE))
            {
                MessageBox.Show("Update thất bại");
                return false;
            }
            else
                MessageBox.Show("Update thành công");
            return true;
        }

        public DataTable GetHangB()
        {
            DataTable dtb = new DataTable();
            dtb = _HangDAO.GetTableHang(_HangE.IDHang);
            return dtb;
        }

        public DataTable getAllHang()
        {
            DataTable dtb = new DataTable();
            dtb = _HangDAO.dtbHang("select * from TBL_Hang");
            return dtb;
        }
        #endregion


        public List<HangEntity> getHang()
        {
            List<HangEntity> Hang = new List<HangEntity>();
            DataTable tbl = new DataTable();
            tbl = _HangDAO.searchByID("");

            if (tbl != null)
                foreach (DataRow r in tbl.Rows)
                {
                    HangEntity nHang = new HangEntity();
                    nHang.IDHang = r["IDHang"].ToString();
                    nHang.ChatLieu = r["ChatLieu"].ToString();
                    nHang.DonViTinh = r["DonViTinh"].ToString();
                    nHang.Loai = r["Loai"].ToString();
                    nHang.MoTaHang = r["MoTaHang"].ToString();
                    nHang.Nguong = Convert.ToInt32(r["Nguong"]);
                    nHang.NguonGoc = r["NguonGoc"].ToString();
                    nHang.PhanLoai = r["PhanLoai"].ToString();
                    nHang.Size = Convert.ToInt32(r["Size"]).ToString();
                    nHang.SoLuongTon = Convert.ToInt32(r["SoLuongTon"]);
                    nHang.TenHang = r["TenHang"].ToString();
                    Hang.Add(nHang);
                }
            return Hang;
        }
        //>>>>>>> .r85
    }
}
